package at.mri.dogwatch.client.pages.newvideo;

import java.util.Set;

import javax.validation.ConstraintViolation;

import at.mri.dogwatch.client.pages.layout.MainPagePresenter;
import at.mri.dogwatch.client.place.ClientPlaceManager;
import at.mri.dogwatch.client.place.LoggedInGateKeeper;
import at.mri.dogwatch.client.place.NameTokens;
import at.mri.dogwatch.shared.proxy.VideoProxy;
import at.mri.dogwatch.shared.services.VideoRequestFactory;
import at.mri.dogwatch.shared.services.VideoRequestFactory.VideoRequestContext;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.UseGatekeeper;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;

public class NewVideoPresenter extends
		Presenter<NewVideoPresenter.NewVideoView, NewVideoPresenter.MyProxy> implements ClickHandler{

	public interface NewVideoView extends View {
		public abstract HasClickHandlers getPostButton();

		public abstract void edit(VideoProxy video);

		public abstract void flush();

		public abstract void disable();

		public abstract void enable();
	}

	@Inject PlaceManager placeManager;
	
	@ProxyCodeSplit
	@NameToken(NameTokens.newvideo)
	@UseGatekeeper(LoggedInGateKeeper.class)
	public interface MyProxy extends ProxyPlace<NewVideoPresenter> {
	}
	
	@Inject
	VideoRequestFactory requestFactory;
	private VideoProxy video;
	private VideoRequestContext videoRequest;

	@Inject
	public NewVideoPresenter(final EventBus eventBus, final NewVideoView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent
				.fire(this, MainPagePresenter.SLOT_SetCenterContent, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
		getView().getPostButton().addClickHandler(this);
	}
	
	@Override
	protected void onReset() {
		super.onReset(); 
		videoRequest = requestFactory.videoRequest();
		video = videoRequest.create(VideoProxy.class);
		video.setTitle("testTitle");
		video.setDescription("testDescription");
		video.setAuthor(null);
		video.setYoutubeTag("b72Z4h2QHuU");
		getView().edit(video);
	}

	@Override
	public void onClick(ClickEvent event) {
		//submit video button was clicked
		//import the data from the view into the video-proxy
		getView().disable();
		getView().flush();
		//send the data to the server
		videoRequest.save(video).fire(new Receiver<Void>() {
			
			@Override
			public void onSuccess(Void response) {
				getView().enable();
				placeManager.revealPlace(new PlaceRequest(NameTokens.feed), true);
			}
			
			@Override
			public void onFailure(ServerFailure error) {
				getView().enable();
				super.onFailure(error);
			}
			
			@Override
			public void onConstraintViolation(
					Set<ConstraintViolation<?>> violations) {
				getView().enable();
				super.onConstraintViolation(violations);
			}
		
		});
	}
	
	
}
